package bysj.service;

import bysj.dao.ActorDao;
import bysj.domain.authority.Actor;
import bysj.domain.authority.ActorAssRole;
import bysj.domain.authority.Role;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public class ActorService {
    private static final ActorDao actorDao = ActorDao.getInstance();
    private static final ActorService actorService = new ActorService();


    public static ActorService getInstance() {
        return actorService;
    }

    public static void main(String[] args) throws Exception {
        //具体类型的对象的添加，请在对应的Service中测试。

        Actor actor = ActorService.getInstance().find(1);
        System.out.println(actor);
        actor = ActorService.getInstance().find(5);
        System.out.println(actor);
    }

    //  不会查找一个Actor集合
    //	不会增加一个Actor
    //	不会修改一个Actor
    //	不会删除一个Actor


    public Actor find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Actor actor = actorDao.find(connection,id);
        JdbcHelper.close(connection);
        return actor;
    }

    /**
     * 获得参数Actor对象应对的所有Role，后者又关联着Resource
     *
     * @param actor
     * @return
     * @throws SQLException
     */
    public Collection<Role> getRoles(Actor actor) throws SQLException {
        Collection<ActorAssRole> actorAssRoles = ActorAssRoleService.getInstance().findAll(actor);
        System.out.println(actorAssRoles);
        Collection<Role> roles = new TreeSet<Role>();
        for (ActorAssRole actorAssRole : actorAssRoles) {
            roles.add(actorAssRole.getRole());
        }
        return roles;
    }
}
